package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.SetmealDto;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealGroup;
import com.itheima.service.CheckgroupService;
import com.itheima.service.CheckitemService;
import com.itheima.service.SetmealGroupService;
import com.itheima.service.SetmealService;
import com.itheima.support.PageResult;
import com.itheima.support.QueryPageBean;
import com.itheima.utils.FileUtil;
import com.itheima.vo.CheckgroupVO;
import com.itheima.vo.SetmealVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@DubboService
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Resource
    private SetmealGroupService setmealGroupService;

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Resource
    private CheckgroupService checkgroupService;

    @Resource
    private CheckitemService checkitemService;

    @Override
    @Transactional
    public boolean saveSetmeal(SetmealDto setmealDto) {
        //1.保存套餐信息
        this.saveOrUpdate(setmealDto.getSetmeal());
        //从垃圾箱撤回垃圾图片
        if(ObjectUtils.isEmpty(setmealDto.getSetmeal().getImg()) == false){
            redisTemplate.opsForSet().remove("file:temp-rubbishset",setmealDto.getSetmeal().getImg());
        }
        //2.删除套餐关系表
        setmealGroupService.deleteBySetmealId(setmealDto.getSetmeal().getId());
        //3.维护关系表
        if(setmealDto.getGroupIds() != null){
            List<SetmealGroup> list = new ArrayList<>();
            for(Long groupId : setmealDto.getGroupIds()){
                SetmealGroup setmealGroup = new SetmealGroup();
                setmealGroup.setGroupId(groupId);
                setmealGroup.setSetmealId(setmealDto.getSetmeal().getId());
                list.add(setmealGroup);
            }
            setmealGroupService.saveBatch(list);
        }
        return true;
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        if(ObjectUtils.isEmpty(queryPageBean.getQueryString()) == false){
            queryWrapper.and((wrapper) -> {
                String clause = queryPageBean.getQueryString();
                wrapper.like("code",clause).or()
                        .like("name",clause).or()
                        .like("help_code",clause);
            });
        }
        IPage<Setmeal> result = this.page(new Page<Setmeal>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize()),queryWrapper);
        return new PageResult(result.getTotal(),result.getRecords());
    }

    @Override
    public void clearOssPic() {
        Set<Object> set = redisTemplate.opsForSet().members("file:temp-rubbishset");
        if(set != null){
            for(Object obj : set){
                String path = (String) obj;
                String key = "file:" + path;
                if(redisTemplate.hasKey(key) == false){
                    FileUtil.delete(path);
                    redisTemplate.opsForSet().remove("file:temp-rubbishset",path);
                }
            }
        }
    }

    @Override
    public SetmealVO findById(Long setmealId) {
//        SetmealVO setmealVO = new SetmealVO();
//        Setmeal setmeal = this.getById(setmealId);
//        BeanUtils.copyProperties(setmeal,setmealVO);
        SetmealVO setmealVO = baseMapper.selectById(setmealId);
        //根据套餐ID查询检查组列表
        setmealVO.setCheckGroups(checkgroupService.findBySetmealId(setmealId));
        if(setmealVO.getCheckGroups() != null){
            //维护检查组中的检查项
            for(CheckgroupVO checkgroupVO : setmealVO.getCheckGroups()){
                checkgroupVO.setCheckItems(checkitemService.findByGroupId(checkgroupVO.getId()));
            }
        }
        return setmealVO;
    }

}